package com.yoyo.itravel.web.controller;

import com.yoyo.itravel.bean.ResultInfo;
import com.yoyo.itravel.bean.User;
import com.yoyo.itravel.constants.UserConstant;
import com.yoyo.itravel.exceptions.SightCollectException;
import com.yoyo.itravel.exceptions.SightException;
import com.yoyo.itravel.service.ISightCollectService;
import com.yoyo.itravel.service.ISightService;
import com.yoyo.itravel.utils.JSONUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * @Description 景点收藏服务端
 *
 * @author HZX
 * @date 2020/5/20 22:44
 */
@Controller
@RequestMapping("/sight_collect")
public class SightCollectController {

    @Autowired
    private ISightCollectService sightCollectService;
    @Autowired
    private ISightService sightService;

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 收藏景点
     */
    @RequestMapping("/collectSight")
    protected void collectSight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得景点id
        String sightId = request.getParameter("sightId");

        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得用户对象
        User user = (User)session.getAttribute(UserConstant.USER);

        if (user == null) {
            // 如果用户未登录，则跳转登录页面
            response.sendRedirect(request.getContextPath() + "/pages/common/login.jsp");
            // 返回
            return;
        }

        try {
            // 添加收藏数据
            sightCollectService.addCollect(user.getUserId(), sightId, new Date());
            // 未出现异常，则收藏成功
            // 该景点被收藏次数加1
            sightService.updateCollectCount(sightId, true);
            // 设置标志位为true
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightCollectException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @Description 判断当前用户是否收藏该景点
     *
     * @Param request
     * @Param response
     * @Return void
     */
    @RequestMapping("/isCollect")
    protected void isCollect(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得景点id
        String sightId = request.getParameter("sightId");

        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得用户对象
        User user = (User)session.getAttribute(UserConstant.USER);

        try {
            if (user == null) {
                // 如果用户为登录，则将标志位设为false，即前端景点详情页面收藏按钮默认显示点击收藏
                info.setFlag(false);
            } else { // 用户已登录
                // 查找收藏数据
                Map<String, Object> map = sightCollectService.findSightCollect(user.getUserId(), sightId);
                if (map == null) {
                    // 用户未收藏
                    info.setFlag(false);
                } else {
                    // 用户已收藏
                    info.setFlag(true);
                }
            }

            JSONUtils.writeValueToResponse(info, response);
        } catch (SightCollectException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @Description 取消收藏景点
     *
     * @Param request
     * @Param response
     * @Return void
     */
    @RequestMapping("/cancelCollectSight")
    protected void cancelCollectSight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获得景点id
        String sightId = request.getParameter("sightId");

        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得用户对象
        User user = (User)session.getAttribute(UserConstant.USER);

        if (user == null) {
            // 如果用户未登录，则跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            // 返回
            return;
        }

        try {
            // 删除收藏数据
            sightCollectService.deleteCollect(user.getUserId(), sightId);
            // 取消收藏成功
            // 该景点被收藏次数减1
            sightService.updateCollectCount(sightId, false);
            info.setFlag(true);
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightCollectException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        } catch (SightException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }

    /**
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     * @Description 查找用户收藏的所有景点
     */
    @RequestMapping("userCollectAllSight")
    protected void userCollectAllSight(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ResultInfo info = new ResultInfo();
        // 获得session对象
        HttpSession session = request.getSession();
        // 获得用户对象
        User user = (User)session.getAttribute(UserConstant.USER);

        if (user == null) {
            // 如果用户未登录，则跳转登录页面
            response.sendRedirect(request.getContextPath() + "/login.jsp");
            // 返回
            return;
        }

        try {
            // 查找用户收藏数据
            Map<String, Object> map = sightCollectService.findSightCollect(user.getUserId(), null);
            // 查找用户收藏成功
            // JSONUtils.writeValueToResponse(allCollect, response);
        } catch (SightCollectException e) {
            e.printStackTrace();
            info.setFlag(false);
            info.setErrorMsg(e.getMessage());
            JSONUtils.writeValueToResponse(info, response);
        }
    }


}
